<?php

namespace App\Http\Controllers\Web;
use App\Http\Controllers\WebController as Controller; 

use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request; 
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;
class HomeController extends Controller
{

    public function __construct(){
        parent::__construct();
        redis::set('a','b');
    }
    
    public function __invoke(){
        return $this->index(func_get_args());
    }

    /**
     * 首页
     * @author wudi
     */
    public function index(){
        $list = DB::table('theme')->orderBy('id','desc')->paginate(10);
        return view('Web.Home.index')->with([
           'list' => $list,
        ]);
    }
    
    public function article($id){
        $data = DB::table('theme')->where('id',$id)->first();
        if(!$data){ abort(404); }
        DB::table('cat')->get();

        #观看人数加入redis
        $ip = request()->ip();
        Redis::hset('global_views',$ip.'-'.$id,time());
        $data->views = $data->views+1;
        #关联文章
        $relation_article = Cache::get('relation_article_'.$id);
        if($relation_article===null){
            $relation_article = DB::table('theme')->select('id','theme_id','title')->where('id','<>',$id)->where('theme_id',$data->theme_id)->orderBy('views','desc')->limit(10)->get();
                Cache::set('relation_article_'.$id, $relation_article,30);
        }
        #评论
        $comment = DB::table('comment')->where('aid',$data->id)->where('status',1)->orderBy('id','desc')->paginate(10);
        return view('Web.Home.article')->with([
            'data'=>$data,
            'bodyClass'=>'single',
            'comment'=>$comment,
            'relation_article'=>$relation_article,
        ]);
    }

    public function cat($id){
        $bool = $this->validate(request(), [
                'page' => 'nullable|numeric',
        ]);
        $input = request()->only(['id', 'page']);
        $list = DB::table('theme')->where('theme_id',$id)->orderBy('id','desc')->paginate(10);
        return view('Web.Home.cat')->with([
            'list'=>$list,
            #'bodyClass'=>'single',
        ]);
    }

    public function search(){
        $bool = $this->validate(request(), [
                'page' => 'nullable|numeric',
                'key'=>'nullable',
        ]);
        $input = request()->only(['page','key']);
        $obj = DB::table('theme');
        if(!empty($input['key'])){
            $input['key'] = trim($input['key']);
            $obj->where('title','like','%'.$input['key'].'%')->orWhere('content','like','%'.$input['key'].'%');
        }
        $list = $obj->orderBy('id','desc')->paginate(10);
        if(!empty($input['key'])){
            $list->appends([
                'key'=>$input['key'],
            ]);
        }
        
        return view('Web.Home.cat')->with([
            'list'=>$list,
        ]);
    }


    public function ajax_comment($id){
        $this->validate(request(), [
                'content' => 'required',
        ]);
        #文章是否存在
        $has = DB::table('theme')->select('id')->where('id',$id)->first();
       
        if(!$has){
            die();
        }
        $input = request()->only(['content']);
        $add_date = date('Y-m-d H:i:s');
        DB::table('comment')->insert([
            'aid'=>$id,
            'add_date'=>$add_date,
            'ip'=>request()->ip(),
            'status'=>1,
            'content'=>$input['content'],
        ]);
        return view('Web.Home.ajax_comment')->with([
            'content'=>$input['content'],
            'ip'=>request()->ip(),
            'add_date'=>$add_date,
        ]);
    }

    public function js(){
        return view('Web.Home.js')->with([
            'js'=>"<script>alert('test');</script>",
        ]);
    }

    public function test(){

        phpinfo();exit;
        $model = new banner();
        $model = new theme();
        $rs = $model->get_theme();

        dump($rs);
    }
}
